package com.imooc.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.imooc.mapper.AdminMapper;
import com.imooc.pojo.Admin;
import com.imooc.pojo.BO.AdminBO;
import com.imooc.service.AdminService;
import com.imooc.utils.MD5Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 慕聘网运营管理系统的admin账户表，仅登录，不提供注册 服务实现类
 * </p>
 *
 * @author 风间影月
 * @since 2025-03-21
 */
@Service
public class AdminServiceImpl extends ServiceImpl<AdminMapper, Admin> implements AdminService {


    @Autowired
    private AdminMapper adminMapper;

    @Override
    public boolean adminLogin(AdminBO adminBO) {

        // 根据用户名获得盐 slat
        Admin admin = getSelfAdmin(adminBO.getUsername());

        if (admin == null) {
            return false;
        } else {
            String md5Str = MD5Utils.encrypt(adminBO.getPassword(), admin.getSlat());

            return md5Str.equals(admin.getPassword());
        }
    }

    @Override
    public Admin getAdminInfo(AdminBO adminBO) {
        return getSelfAdmin(adminBO.getUsername());
    }

    private Admin getSelfAdmin(String username) {
        return adminMapper.selectOne(new QueryWrapper<Admin>()
                .eq("username", username));
    }
}
